<template>
  <div>
    <!-- 搜索区域 -->
    <div class="search-bar">
        <div style="display: flex; justify-content: flex-start;">
          <el-form :inline="true" :model="searchForm" class="demo-form-inline">
            <el-form-item label="搜索：">
            <el-select v-model="searchForm.condition" placeholder="请选择" style="width: 120px;">
                <el-option label="全部" value="all"></el-option>
                <el-option label="用户名" value="playmateNickname"></el-option>
                <el-option label="编号" value="playmateId"></el-option>
                <el-option label="地址" value="addressName"></el-option>
                <el-option label="性别" value="playmateSex"></el-option>
            </el-select>
            </el-form-item>

            <el-form-item>
            <el-input v-model="searchForm.keyword" placeholder="请输入搜索内容" style="width: 160px;"></el-input>
            </el-form-item>

            <el-form-item label="状态搜索：">
            <el-select v-model="searchForm.statusCondition" placeholder="请选择" style="width: 120px;">
                <el-option label="全部" value="null"></el-option>
                <el-option label="空闲" value="0"></el-option>
                <el-option label="游戏中" value="1"></el-option>
                <el-option label="下线" value="2"></el-option>
            </el-select>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="handleSearch" style="height: 32px; padding: 0 12px; font-size: 12px;">搜索</el-button>
            </el-form-item>

            <el-form-item>
              <el-button @click="resetSearch" style="height: 32px; padding: 0 12px; font-size: 12px;">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
    </div>

    <!-- 陪玩列表 -->
    <el-table :data="playmateList" height="510" border style="width: 100%">
        <el-table-column 
            :label="header" 
            v-for="header  in tableHeader" :key="header" 
            :prop="getPropName(header)"
            width="150" show-overflow-tooltip>
        </el-table-column>

        <!-- <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="info(scope.row)">详情</el-button>  
          </template>
        </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <div class="block">
        <el-pagination
            @size-change="changePageSize"               
            @current-change="handleCurrentChange"
            :current-page="params.pageNum"
            :page-sizes="[2, 3, 4, 5,7,9]"
            :page-size="params.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="count">
        </el-pagination>
    </div>    
  </div>
</template>

<script>
export default {
  data(){
    return{
      tableHeader:['陪玩ID','昵称', '性别', '电话','邮箱','保证金','游戏状态','游戏时长','游戏爱好','评分', '地址', '创建时间'], 
      playmateList:[],
      count:0,        //总条数
      params:{
          pageNum:1,      //当前页
          pageSize:9,     //页容量
      },
      searchForm: {
        condition: 'all', // 搜索条件字段名
        statusCondition: '',  // 状态搜索条件字段名
        keyword: ''    // 搜索关键词
      },
    }
  },
  methods:{
    // 根据表头获取对应的属性名
    getPropName(header){
      const mapping = {
        '陪玩ID': 'id',
        '昵称': 'playmateNickname',
        '性别': 'playmateSex',
        '电话': 'userTelephone',
        '邮箱':'playmateEmail',
        '保证金':'playmateMarginMoney',
        '游戏状态': 'playmateGameStatus',
        '游戏时长':'playmateGameTime',
        '游戏爱好':'playmateGameHobby',
        '评分':'playmateRating',
        '地址': 'addressName',
        '创建时间': 'createTime'
      }
      return mapping[header];
    },
    // 加载陪玩列表数据
    async loadPlaymateList() { 
      const requestData = {
          pageNum: this.params.pageNum,
          pageSize: this.params.pageSize,
          tenantId: 1 
        };
        if (this.searchForm.statusCondition !== null) {
          requestData.gameStatus = parseInt(this.searchForm.statusCondition);
        }
        if (this.searchForm.condition !== 'all' && this.searchForm.keyword){
          switch(this.searchForm.condition){
            case 'playmateId':
              requestData.playmateId = parseInt(this.searchForm.keyword);
              break;
            case 'playmateNickname':
              requestData.playmateNickname = this.searchForm.keyword;
              break;
            case 'addressName':
              requestData.addressName = this.searchForm.keyword;
              break;
            case 'playmateSex':
              requestData.playmateSex = this.searchForm.keyword === '男' ? 1 : 0;
              break;
          }
        }
        let resp = await this.customerServiceApi.playmateList(requestData)
        if(resp.code == 200) {
          this.playmateList = resp.data.list.map(item => {
            return {
              ...item,
              playmateSex: item.playmateSex === 1 ? '男' : '女',
              playmateGameStatus: this.formatGameStatus(item.playmateGameStatus)
            };
          });
          this.count = resp.data.total;
        }else{
          this.$message.error(resp.message);
        }
    },
    // 格式化游戏状态显示
    formatGameStatus(status) {
      const statusMap = {
        0: '空闲',
        1: '游戏中',
        2: '下线'
      };
      return statusMap[status] || '未知';
    },
    //搜索
    handleSearch() {
      this.params.pageNum = 1;
      this.loadPlaymateList();
    },
    // 重置搜索条件
    resetSearch() {
      this.searchForm.condition = 'all'; // 清空第一个搜索条件
      this.searchForm.statusCondition = ''; // 清空状态搜索条件
      this.searchForm.keyword = ''; // 清空关键词
      this.params.pageNum = 1;
      this.loadPlaymateList(); // 重新加载列表（可选）
    },
     // 改变页大小
    changePageSize(val) {
      this.params.pageSize = val;
      this.params.pageNum = 1; // 重置到第一页
      this.loadPlaymateList();
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.params.pageNum = val;
      this.loadPlaymateList();
    }
  },
  created(){
    this.loadPlaymateList();
  }
};
</script>

<style scoped>
.search-bar {
  padding: 5px 0; /* 减小上下内边距 */
}

.demo-form-inline {
  display: flex;
  align-items: center;
  gap: 8px; /* 减小组件之间的间距 */
}

.el-form-item {
  margin-top: 0; /* 移除默认的顶部外边距 */
  margin-bottom: 0; /* 移除默认的底部外边距 */
}

.el-select .el-input__inner {
  height: 30px; /* 进一步减小高度 */
  font-size: 12px;
}

.el-input .el-input__inner {
  height: 30px; /* 进一步减小高度 */
  font-size: 12px;
}

.el-button {
  height: 30px; /* 进一步减小高度 */
  padding: 0 10px; /* 减小内边距 */
  font-size: 12px;
}
</style>
